package com.qinsong.myapplication.controller.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.qinsong.myapplication.R;
import com.qinsong.myapplication.controller.fragment.ChatFragment;
import com.qinsong.myapplication.controller.fragment.ContactListFragment;
import com.qinsong.myapplication.controller.fragment.SettingFragment;
import com.qinsong.myapplication.utils.ScreenUtils;

public class indexActivity extends FragmentActivity {
    private RadioGroup radioMain ;
    private ChatFragment chatFragment;
    private ContactListFragment contactListFragment;
    private SettingFragment settingFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        ScreenUtils.setFullScreen(this, true);
        initView();
        initData();
        initListener();
    }
    //
    private void initListener() {
        //选择事件监听
        radioMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Fragment fragment = null;
                switch (checkedId){
                    //会话页面
                    case R.id.conversation:
                        fragment=chatFragment;
                        break;
                    //联系人页面
                    case R.id.lianxiren:
                        fragment=contactListFragment;
                        //设置页面
                        break;
                    case R.id.setting:
                        fragment=settingFragment;
                        break;
                }
                //实现切换方法
                switchFragment(fragment);
            }
        });
        //默认选择会话fragment
        radioMain.check(R.id.conversation);
    }
    //实现fragment切换方法
    private void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fl_main,fragment).commit();
    }

    private void initData() {
        //创建3个fragment
        chatFragment = new ChatFragment();
        contactListFragment = new ContactListFragment();
        settingFragment = new SettingFragment();
    }

    //控件初始化
    private void initView() {
        radioMain=findViewById(R.id.radio_main);
    }
}
